package tech.abing.spider.task.pageprocessor.amac;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.DirectInvestmentFundStatEntity;
import tech.abing.spider.task.model.amac.PrivateFundStatEntity;
import tech.abing.spider.util.UuIdPublic;

public class DirectInvestmentFundProcess implements PageProcessor{

	private static final String baseUrl = "http://gs.amac.org.cn/amac-infodisc/res/aoin/product/";
	
	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		String json = page.getContent();
		
		JSONObject jsonObj = JSONObject.fromObject(json);
		
		boolean first = jsonObj.getBoolean("first");
		
		if(first){
			//总页数
			long totalPages = jsonObj.getLong("totalPages");
			
			long totalCount = jsonObj.getLong("totalElements");
			DirectInvestmentFundStatEntity stat = new DirectInvestmentFundStatEntity();
			stat.setId(UuIdPublic.timeStrongID(5));
			stat.setDirectInvestmentFundCount(String.valueOf(totalCount));
			stat.setCreateTime(new Date());
			objectContainer.add(stat);
			
			PageRequest request = page.getRequest();
			for(int i = 1; i <= totalPages; i++){
				String url = request.getUrl().replace("&page=0&", "&page="+i+"&");
				PageRequest req = context.createPageRequest(url, DirectInvestmentFundProcess.class);
				
				req.setMethod(request.getMethod());
				req.setPostType(request.getPostType());
				req.setReqPayloadParams(request.getReqPayloadParams());
				
				req.setHeaders(request.getHeaders());
				
				queue.add(req);
			}
		}
		
		//解析数据
		JSONArray content = jsonObj.getJSONArray("content");
		if(content !=null && content.size()>0){
			for(int i = 0;i<content.size();i++){
				JSONObject ctxObj = content.getJSONObject(i);
				String url = ctxObj.getString("id");
				url = baseUrl + url+".html";
				
				PageRequest req = context.createPageRequest(url, DirectInvestmentFundDetailProcess.class);
				
				req.getExtParams().put("ctxObj", ctxObj);
				
				queue.add(req);
			}
		}
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
